import { defineStore } from 'pinia';

export const userStore = defineStore('user', {
  state: () => ({
    userData: null, // 当前用户登录信息
    isSavePwd: false, // 当前是否保存密码
    savedPassword: null, // 保存的密码
  }),

  actions: {
    saveUserInfo(loginInfo, loginForm) {
      this.userData = loginInfo.data;
      if (this.isSavePwd) {
        this.savedPassword = loginForm.password;
        window.localStorage.setItem('savedPassword', loginForm.password);
      }
      window.localStorage.setItem('userData', JSON.stringify(this.userData));
    },
    savePwd(val) {
      this.isSavePwd = val;
      val && window.localStorage.setItem('isSavePwd', val);
    },
    removeUserInfo() {
      this.userData = null;
      this.savedPassword = null;
      window.localStorage.removeItem('userData');
      window.localStorage.removeItem('isSavePwd');
    },
  },
});
